$(function () {
    var chkStyle = $('#chkStyle').val();

    var settings = {
        callback: {
            onCheck: function (event, treeId, treeNode) {
                if (loresetPoints.isChekced(treeNode)) {
                    loresetPoints.put(treeNode);
                } else {
                    loresetPoints.remove(treeNode);
                }
                loresetPoints.toString();
            }
        },
        check: {
            chkboxType: {"Y": "", "N": ""}, // 联动。即选择父时联动选择所有子，反之亦然
            chkStyle: chkStyle,
            enable: true
        },
        data: {
            key: {
                name: 'name',
                title: 'name'
            },
            simpleData: {
                enable: true,
                idKey: 'nid',
                pIdKey: 'pid'
            }
        }
    };

    var loresetPoints = {
        nodes: [],
        isChekced: function (node) {
            var cs = node.getCheckStatus();
            return cs.checked && !cs.half;
        },
        put: function (node) {
            if (this.isChekced(node)) {
                var json = {};
                json.id = node.id;
                json.ids = node.id;
                json.name = node.name;
                while (node.pid > 0) {
                    node = node.getParentNode();
                    json['name'] = node.name + ' > ' + json['name'];
                    json['ids'] = node.id + ',' + json['ids'];
                }
                this.nodes.push(json);
            }
        },
        remove: function (node) {
            var nodes = this.nodes;
            var id = node.id;
            var loop = true;
            nodes.forEach(function (v, i) {
                if (loop && v.id == id) {
                    nodes.splice(i, 1);
                    loop = false;
                }
            });
        },
        toString: function () {
            var encode = encodeURIComponent(JSON.stringify(this.nodes));
            $('#loresetPoint').html(encode);
        }
    };

    initLoresetPointTree();

    /**
     * 处理知识体系能被ztree解析
     *
     */
    function handLoresetPonintData() {
        var array = [];
        var nid = 1;
        data.forEach(function (x) {
            array.push({id: x.id, name: x.loresetName, pid: 0, nid: nid++});
            var xChildList = x.loresetPointList;
            if (xChildList && xChildList.length > 0) {
                var xp = nid - 1;
                xChildList.forEach(function (y) {
                    array.push({id: y.id, name: y.pointName, pid: xp, nid: nid++});
                    var yChildList = y.childLoresetPointList;
                    if (yChildList && yChildList.length > 0) {
                        var yp = nid - 1;
                        yChildList.forEach(function (z) {
                            array.push({id: z.id, name: z.pointName, pid: yp, nid: nid++});
                        })
                    }
                });
            }
        });
        return array;
    }

    function initLoresetPointTree() {
        var data = handLoresetPonintData();
        if(data.length > 0 && data != null){
            data.forEach(function (v) {
                v.nocheck = v.pid <= 0;
            });
            var instance = $.fn.zTree.init($('#ztree'), settings, data);
            instance.expandAll(true);
        }else{
            $('#ztree').next('div.noDataTipsBox').css({'display':'block'});
        }
    }

});